import copy
import os

from src.log.logger import logger
from config.constants import PACKAGE_SPDXID, SUPPLIER, \
                      ORIGINATOR, REFERENCELOCATOR, \
                      PACKAGE_CHECK_SUMS
from config.common_enum_class import ReferenceCategory
from src.tools.query import RpmInfoQuery

class SbomPackagesElementGenerate:
    """
    generate sbom packages element info for rpm
    """

    def __init__(self, package_element, rpm_path):
        """
        init package_element, rpm_path
        Args:
            package_element:
            rpm_path:
        Returns:
        """
        
        self._package_data = package_element
        self.rpm_path = rpm_path
        self.rpm_file_name = os.path.basename(rpm_path)
        
    def generate_packages_element(self, rpm_info):
        """
        merge new sbom  element jsons to sbom.json
        Args:
            rpm_info:
        Returns:
        """
        
        packages_element = []
        packages_element.append(self.get_package_data(rpm_info))
        return packages_element
        
    def get_package_data(self, rpm_info):
        """
        get package data
        Args:
            rpm_info
        Returns:
        
        """
        
        package_data = self._package_data
        package_data['name'] = rpm_info.get('Name', "")
        package_data['filename'] = self.rpm_file_name
        package_data['SPDXID'] = PACKAGE_SPDXID.format(rpm_name=package_data['name'], \
                                                                checksum=RpmInfoQuery.get_checksum(self.rpm_path))
        package_data['version'] = rpm_info.get('Version', "")
        package_data['supplier'] = SUPPLIER
        package_data['originator'] = ORIGINATOR
        package_data['downloadLocation'] = self._get_downloadLocation()
        package_data['externalRefs']['referenceCategory'] = ReferenceCategory.PACKAGE_MANAGER
        package_data['externalRefs']['referenceLocator'] = REFERENCELOCATOR.format(rpm_name = package_data['name'], \
                                                                                version = package_data['version'], \
                                                                                arch=rpm_info.get('Architecture', ""), \
                                                                                os_release = "Unknown")
        package_data['externalRefs']['referenceType'] = 'purl'
        package_data['checksums'] = copy.copy(PACKAGE_CHECK_SUMS)
        package_data['checksums']['checksumValue'] = RpmInfoQuery.get_checksum(self.rpm_path)
        
        package_data['homepage'] = rpm_info.get('URL', "")
        package_data['licenseConcluded'] = rpm_info.get('License', "")
        package_data['licenseDeclared'] = rpm_info.get('License', "")
        package_data['licenseComments'] = rpm_info.get('License', "")
        package_data['description'] = rpm_info.get('Description', "")
        return package_data
    
    def _get_downloadLocation(self):
        """
        get downloadLocation field
        Args:
        
        Returns:
        
        """
        
        return "https://repo.huaweicloud.com/openeuler/openEuler-22.03-LTS-SP3/everything/aarch64/Packages"